package com.jiandian.zh.api.common.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jiandian.zh.api.common.domain.BaseDomain;
import com.jiandian.zh.api.common.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.MyMapper;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * @author lik
 */
@Component
public class BaseServiceImpl<T extends BaseDomain,D extends MyMapper<T>> implements BaseService<T> {

    @Autowired(required = false)
    private MyMapper<T> dao;

    @Override
    public int insert(T t,String createBy ) {
        // t.setCreateBy(createBy);
        t.setGmtCreate(Timestamp.valueOf(LocalDateTime.now()));
        return dao.insert(t);
    }

    @Override
    public int delete(T t) {
        return dao.delete(t);
    }

    @Override
    public int update(T t,String updateBy) {
        // t.setModifiedBy(updateBy);
        t.setGmtModified(Timestamp.valueOf(LocalDateTime.now()));
        return dao.updateByPrimaryKeySelective(t);
    }


    @Override
    public T selectOne(T t) {
        return dao.selectOne(t);
    }

    @Override
    public PageInfo<T> selectPage(int pageNum, int pageSize,T t) {
        PageHelper.startPage(pageNum, pageSize);
        return  new PageInfo<>(dao.select(t));
    }

    @Override
    public int selectCount(T t) {
        return dao.selectCount(t);
    }

    @Override
    public T selectByPrimaryKey(String primaryKey){
        return dao.selectByPrimaryKey(primaryKey);
    }

    @Override
    public int deleteByPrimaryKey(String primaryKey) {
        return dao.deleteByPrimaryKey(primaryKey);
    }
}
